package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.system.domain.entity.HomestayBasic;
import com.ruoyi.system.mapper.HomestayBasicMapper;
import com.ruoyi.system.properties.MinioProperties;
import com.ruoyi.system.service.IFileUploadService;
import com.ruoyi.system.service.IHomestayBasicService;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class IFileUploadServiceImpl implements IFileUploadService {

    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String upload(MultipartFile file) {
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            //创建MinioClient对象
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioProperties.getEndpointUrl())
                            .credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
                            .build();

            // Make 'asiatrip' bucket if not exist.
            //创建bucket
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket "+minioProperties.getBucketName()+" already exists.");
            }


            //获取上传文件名称
            // 1每个上传文件名称唯一的uuid生成01.jpg1
            // 2根据当前日期对上传文件进行分组20230910
                    // 202309101.jpg
            String dateDir = DateUtils.format( new Date(), "yyyyMMdd" );
            String uuid = UUID.randomUUID().toString( ) .replaceAll( "-","");

            //获取上传文件名称
            String filename = dateDir+" /"+uuid+file.getOriginalFilename();


            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(minioProperties.getBucketName())
                            .object(filename)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .build());

            //获取上传文件在minio路径
            String url = minioProperties.getEndpointUrl()+ "/"+minioProperties.getBucketName()+"/" + filename;

            return url;
        } catch (Exception e) {
           e.printStackTrace();
           throw new BaseException("文件上传失败！");
        }
    }
}
